{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step1:softmax result=\n",
      "[[0.09003057 0.24472848 0.66524094]\n",
      " [0.09003057 0.24472848 0.66524094]\n",
      " [0.09003057 0.24472848 0.66524094]]\n",
      "step2:cross_entropy result=\n",
      "1.222818\n",
      "Function(softmax_cross_entropy_with_logits) result=\n",
      "1.2228179\n"
     ]
    }
   ],
   "source": [
    "import tensorflow.compat.v1 as tf\n",
    "tf.disable_v2_behavior()\n",
    "\n",
    "logits = tf.constant([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]])\n",
    "y=tf.nn.softmax(logits)\n",
    "y_=tf.constant([[0.0,0.0,1.0],[0.0,0.0,1.0],[0.0,0.0,1.0]])\n",
    "#step2:do cross_entropy \n",
    "cross_entropy = -tf.reduce_sum(y_*tf.log(y))\n",
    "#do cross_entropy just one step\n",
    "cross_entropy2=tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_))#dont forget tf.reduce_sum()!!\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    softmax = sess.run(y)\n",
    "    c_e = sess.run(cross_entropy)\n",
    "    c_e2 = sess.run(cross_entropy2)\n",
    "    print(\"step1:softmax result=\")\n",
    "    print(softmax)\n",
    "    print(\"step2:cross_entropy result=\")\n",
    "    print(c_e)\n",
    "    print(\"Function(softmax_cross_entropy_with_logits) result=\")\n",
    "    print(c_e2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
